package com.lifeservice.console.controller;

import com.lifeservice.common.annotation.Log;
import com.lifeservice.common.core.controller.BaseController;
import com.lifeservice.common.core.domain.AjaxResult;
import com.lifeservice.common.core.domain.entity.SysUser;
import com.lifeservice.common.core.page.TableDataInfo;
import com.lifeservice.common.enums.BusinessType;
import com.lifeservice.common.utils.poi.ExcelUtil;
import com.lifeservice.console.domain.LifeSupplierDraft;
import com.lifeservice.console.domain.LifeSupplierInfo;
import com.lifeservice.console.domain.ScheduleAddBean;
import com.lifeservice.console.domain.req.LifeSupplierDraftReq;
import com.lifeservice.console.service.ILifeScheduleService;
import com.lifeservice.console.service.ILifeSupplierInfoService;
import com.lifeservice.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

/**
 * 商户信息Controller
 *
 * @author lifeservice
 * @date 2023-02-25
 */
@RestController
@RequestMapping("/console/info")
public class LifeSupplierInfoController extends BaseController {
    @Autowired
    private ILifeSupplierInfoService lifeSupplierInfoService;

    @Autowired
    private ILifeScheduleService lifeScheduleService;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询商户信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LifeSupplierInfo lifeSupplierInfo) {
        startPage();
        List<LifeSupplierInfo> list = lifeSupplierInfoService.selectLifeSupplierInfoList(lifeSupplierInfo);
        return getDataTable(list);
    }

    /**
     * 导出商户信息列表
     */
    @Log(title = "商户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LifeSupplierInfo lifeSupplierInfo) {
        List<LifeSupplierInfo> list = lifeSupplierInfoService.selectLifeSupplierInfoList(lifeSupplierInfo);
        ExcelUtil<LifeSupplierInfo> util = new ExcelUtil<LifeSupplierInfo>(LifeSupplierInfo.class);
        util.exportExcel(response, list, "商户信息数据");
    }

    /**
     * 获取商户信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(lifeSupplierInfoService.selectLifeSupplierInfoById(id));
    }

    /**
     * 获取商户审核信息详细信息
     */
    @GetMapping(value = "/draft/{id}")
    public AjaxResult getDraft(@PathVariable("id") String id) {
        return success(lifeSupplierInfoService.selectLifeSupplierDraftById(id));
    }

    /**
     * 新增商户信息
     */
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LifeSupplierInfo lifeSupplierInfo) {
        return toAjax(lifeSupplierInfoService.insertLifeSupplierInfo(lifeSupplierInfo));
    }

    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addLifeSupplierDraft(@RequestBody LifeSupplierDraft lifeSupplierDraft) {
        return toAjax(lifeSupplierInfoService.addLifeSupplierDraft(lifeSupplierDraft));

    }

    /**
     * 修改商户信息
     */
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LifeSupplierInfo lifeSupplierInfo) {
        return toAjax(lifeSupplierInfoService.updateLifeSupplierInfo(lifeSupplierInfo));
    }

    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editDraft")
    public AjaxResult editDraft(@RequestBody LifeSupplierDraft lifeSupplierDraft) {
        return toAjax(lifeSupplierInfoService.updateLifeSupplierDraft(lifeSupplierDraft));
    }

    /**
     * 删除商户信息
     */
    @Log(title = "商户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lifeSupplierInfoService.deleteLifeSupplierInfoByIds(ids));
    }

    @Log(title = "商户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/draft/{ids}")
    public AjaxResult removeDraft(@PathVariable String[] ids) {
        return toAjax(lifeSupplierInfoService.deleteLifeSupplierDraftByIds(ids));
    }

    /**
     * 新增商户员工排班管理
     */
    @Log(title = "商户排班管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSchedule")
    public AjaxResult addSchedule(@RequestBody ScheduleAddBean scheduleAddBean) throws ParseException {
        return toAjax(lifeScheduleService.addSchedule
                (scheduleAddBean));
    }

    /**
     * 查询商户列表（审核的列表）
     */
    @GetMapping("/lifeServiceDraft")
    public TableDataInfo lifeServiceDraft(LifeSupplierDraftReq lifeSupplierDraftReq) {
        startPage();
        List<LifeSupplierDraft> lifeSupplierDrafts = lifeSupplierInfoService.lifeServiceDraft(lifeSupplierDraftReq);
        return getDataTable(lifeSupplierDrafts);
    }


    @GetMapping("/getUserByRole")
    public List<SysUser> getUsersByRole() {
        return iSysUserService.getUserByRoleName("供应商上架审核员");
    }

    @PutMapping("/reviewMerchant")
    public void reviewMerchant(@RequestBody LifeSupplierDraftReq lifeSupplierDraftReq) {
        lifeSupplierInfoService.reviewMerchant(lifeSupplierDraftReq);
    }
}
